package com.dnjn.mes.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.mes.biz.assist.ext.Wrapperz;
import com.dnjn.mes.biz.domain.*;
import com.dnjn.mes.biz.mapper.MesResolvePlanMapper;
import com.dnjn.mes.biz.service.*;
import com.dnjn.mes.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 异常解决方案表 服务实现类
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Service
public class MesResolvePlanServiceImpl extends ServiceImpl<MesResolvePlanMapper, MesResolvePlan> implements IMesResolvePlanService {

    @Autowired
    protected IMesEquipmentService mesEquipmentService;
    @Autowired
    protected IMesAssemblyService mesAssemblyService;
    @Autowired
    protected IMesWorkpieceService workpieceService;
    @Autowired
    protected IMesExceptPlanXrefService mesExceptPlanXrefService;
    @Autowired
    protected IMesExceptService mesExceptService;
    @Override
    public String importData(List<MesResolvePlan> list, boolean updateSupport) throws Exception {
        Integer maxOrdinal = baseMapper.getMaxOrdinal();
        if (Objects.isNull(maxOrdinal)) {
            maxOrdinal = 0;
        }

        MesEquipment mesEquipment = mesEquipmentService.getOne(Wrapperz.lambdaQuery(MesEquipment.class)
                .eq(MesEquipment::getEquipmentName, list.get(0).getEquipmentName()));
        if (null == mesEquipment) {
            throw new Exception("机器为空,查询失败!");
        }

        List<String> assemblyNameList = list.stream().map(MesResolvePlan::getAssemblyName).distinct()
                .collect(Collectors.toList());
        for (String name : assemblyNameList) {
            name =StringUtils.trimStr(name);

            MesAssembly assembly = mesAssemblyService.getOne(Wrapperz.lambdaQuery(MesAssembly.class)
                    .eq(MesAssembly::getAssemblyName,name));
            if (null == assembly) {
                MesAssembly mesAssembly = new MesAssembly();
                mesAssembly.setAssemblyName(name);
                mesAssembly.setEquipmentId(mesEquipment.getEquipmentId());
                Integer assemblyOrdinal = mesAssemblyService.getMaxOrdinal();
                assemblyOrdinal++;
                mesAssembly.setOrdinal(assemblyOrdinal);
                mesAssemblyService.save(mesAssembly);
            }
        }
        String msg = "请检查零部件是否存在:";
        for (MesResolvePlan vo : list) {
            /*MesWorkpiece workpiece = workpieceService.queryBy(vo.getEquipmentName(), vo.getAssemblyName(),
                    vo.getWorkpieceName());*/
            //根据方案进行判空
            String description = vo.getDescription();
            if (StringUtils.isNotEmpty(description)) {
                vo.setDescription(StringUtils.trimStr(description));
                long count = baseMapper.selectCount(
                        Wrappers.<MesResolvePlan>lambdaQuery().eq(MesResolvePlan::getDescription,description));
                if(count > 0 ){
                    continue;
                }
            }

            vo.setAssemblyName(StringUtils.trimStr(vo.getAssemblyName()));
            //关联工件id
            MesWorkpiece workpiece = workpieceService.queryBy(vo.getEquipmentName(), vo.getAssemblyName(),
                    vo.getWorkpieceName());
            if(null == workpiece){
                msg += vo.getWorkpieceName()+"-"+vo.getAssemblyName()+"|";
                continue;
            }
            vo.setWorkpieceId(workpiece.getWorkpieceId());//?
            maxOrdinal++;
            vo.setOrdinal(maxOrdinal);
            vo.setEquipmentId(mesEquipment.getEquipmentId());
            save(vo);
        }
        if (!msg.equals("请检查零部件:")) {
            return msg;
        }
        return "OK";
    }

    @Override
    @Transactional
    public int savePlan(MesResolvePlan domain) {
        //设置 ordinal
        Integer rodinal = 0;
        Integer maxOrdinal = baseMapper.getMaxOrdinal();
        if (!Objects.isNull(maxOrdinal)) {
            rodinal = maxOrdinal;
        }
        domain.setOrdinal(rodinal+1);
        /*MesExceptPlanXref mesExceptPlanXref = new MesExceptPlanXref();
        mesExceptPlanXref.setPlanId(domain.getPlanId());
        mesExceptPlanXref.setExceptId(domain.getExceptId());*/
        //关联存入异常表的工件id,更新异常表工件的话，异常工件对应关系1-1？
        /*MesExcept except = new MesExcept();
        except.setExceptId(domain.getExceptId());
        except.setWorkpieceId(domain.getWorkpieceId());
        mesExceptService.updateById(except);*/

        //关联计划异常表
        return baseMapper.insert(domain);
    }

    @Override
    public List<MesResolvePlan> selectPlanList(MesResolvePlan domain) {
        return baseMapper.selectPlanList(domain);
    }

    @Override
    public Long removePlanById(Long planId) {
        return baseMapper.removePlanById(planId);
    }

    @Override
    public List<MesResolvePlan> selectAllPlanList(MesResolvePlan domain) {
        return baseMapper.selectAllPlanList(domain);
    }

    @Override
    public List<MesResolvePlan> getPlanByWorkpieceId(MesResolvePlan domain) {
        return baseMapper.getPlanByWorkpieceId(domain);
    }

    @Override
    public List<MesResolvePlan> getPlanPageByExcepId(Long exceptId) {
        MesResolvePlan mesResolvePlan = new MesResolvePlan();
        mesResolvePlan.setExceptId(exceptId);
        List<MesResolvePlan> planList = baseMapper.selectPlanList(mesResolvePlan);
       /* List<MesResolvePlan> list = new ArrayList<>();
        List<MesExceptPlanXref> mesExceptPlanXrefList = mesExceptPlanXrefService.lambdaQuery().eq(MesExceptPlanXref::getExceptId, exceptId).list();
        if(!mesExceptPlanXrefList.isEmpty()){
            for (MesExceptPlanXref mesExceptPlanXref : mesExceptPlanXrefList) {
                list.add(baseMapper.selectById(mesExceptPlanXref.getPlanId()));
            }
        }*/
        return planList;
    }

    @Override
    public List<MesResolvePlan> getPlansByAssemblyId(Long assemblyId,String searchKey,Long equipmentId) {
        return baseMapper.getPlansByAssemblyId(assemblyId,searchKey,equipmentId);
    }

    @Override
    public MesResolvePlan getPlan(Long planId) {
        return baseMapper.getPlan(planId);
    }

    @Override
    public List<MesResolvePlan> getPlanPageByWorkpieceId(Long workpieceId,Long equipmentId) {
        return baseMapper.getPlanPageByWorkpieceId(workpieceId,equipmentId);
    }
}
